package com.wanxi.service.Impl;

import com.wanxi.dao.Impl.TeacherDaoImpl;
import com.wanxi.dao.TeacherDao;
import com.wanxi.dto.ResultDTO;
import com.wanxi.model.ProductType;
import com.wanxi.model.Teacher;
import com.wanxi.service.TeacherService;
import com.wanxi.util.JSONCopy;
import com.wanxi.vo.TeacherVO;

import java.util.Iterator;
import java.util.List;

public class TeacherServiceImpl implements TeacherService {
    TeacherDao teacherDao=new TeacherDaoImpl();
    @Override
    public ResultDTO findAllTeacher() {
        List<Teacher> list=teacherDao.findAllTeacher();
        if(list.size()>0){
            return ResultDTO.getDTO(list.size(),list);
        }
        return ResultDTO.builder().data(list).msg("no data").count(0).code(1).build();
    }

    @Override
    public ResultDTO findTeacherById(TeacherVO teacherVO) {
        Teacher teacher= JSONCopy.copy(teacherVO, Teacher.class);
        Teacher t=teacherDao.findTeacherById(teacher);
        if(t!=null){
            return ResultDTO.getDTO(1,t);
        }
        return ResultDTO.builder().count(0).msg("error").code(1).build();
    }

    @Override
    public ResultDTO updateEnable(TeacherVO teacherVO) {
        Teacher teacher= JSONCopy.copy(teacherVO, Teacher.class);
        int i=teacherDao.updateEnable(teacher);
        if(i==1){
            return ResultDTO.getDTO(i,teacherVO);
        }
        return ResultDTO.builder().data(teacherVO).count(0).code(1).msg("error").build();
    }

    @Override
    public ResultDTO addTeacher(TeacherVO teacherVO) {
        Teacher teacher=JSONCopy.copy(teacherVO,Teacher.class);
        int i=teacherDao.addTeacher(teacher);
        if(i==1){
            return ResultDTO.getDTO(i,teacherVO);
        }
        return ResultDTO.builder().data(teacherVO).count(0).code(1).msg("error").build();
    }

    @Override
    public ResultDTO update(TeacherVO teacherVO) {
        Teacher teacher=JSONCopy.copy(teacherVO, Teacher.class);
        int i=teacherDao.updateTeacher(teacher);
        if(i==1){
            return ResultDTO.getDTO(i,teacherVO);
        }
        return ResultDTO.builder().data(teacherVO).count(0).code(1).msg("error").build();
    }

    @Override
    public ResultDTO delete(TeacherVO teacherVO) {
        Teacher teacher=JSONCopy.copy(teacherVO, Teacher.class);
        int i=teacherDao.deleteTeacher(teacher);
        if(i==1){
            return ResultDTO.getDTO(i,teacherVO);
        }
        return ResultDTO.builder().data(teacherVO).count(0).code(1).msg("error").build();
    }

    @Override
    public ResultDTO findTeacher() {
        List<Teacher> list=teacherDao.findAllTeacher();
        if(list.size()>0){
            Iterator<Teacher> iterator=list.iterator();
            while (iterator.hasNext()){
                Teacher t= iterator.next();
                if (t.getEnable()==0)
                    iterator.remove();
            }
            return ResultDTO.getDTO(list.size(),list);
        }
        return ResultDTO.builder().data(list).msg("no data").count(0).code(1).build();
    }
}
